package com.ruoyi.web.ydz.service.impl;

import java.util.List;
import java.util.Objects;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.web.ydz.domain.DrugDisease;
import com.ruoyi.web.ydz.service.IDrugDiseaseService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.ydz.mapper.DrugMapper;
import com.ruoyi.web.ydz.domain.Drug;
import com.ruoyi.web.ydz.service.IDrugService;

import javax.annotation.Resource;

/**
 * 药品Service业务层处理
 * 
 * @author drx
 * @date 2024-10-16
 */
@Service
public class DrugServiceImpl implements IDrugService 
{
    @Autowired
    private DrugMapper drugMapper;

    @Autowired
    private IDrugDiseaseService drugDiseaseService;

    @Resource
    private ISysDictDataService iSysDictDataService;

    /**
     * 查询药品
     * 
     * @param id 药品主键
     * @return 药品
     */
    @Override

    public Drug selectDrugById(Long id)
    {
        DrugDisease drugDisease = new DrugDisease();
        drugDisease.setDrugId(id);
        List<DrugDisease> list =  drugDiseaseService.selectDrugDiseaseList(drugDisease);
        Drug drug = drugMapper.selectDrugById(id);
        drug.setDrugDiseaseList(list);
        return drug;
    }

    /**
     * 查询药品列表
     * 
     * @param drug 药品
     * @return 药品
     */
    @Override
    public List<Drug> selectDrugList(Drug drug)
    {
        return drugMapper.selectDrugList(drug);
    }

    /**
     * 新增药品
     * 
     * @param drug 药品
     * @return 结果
     */
    @Override
    public int insertDrug(Drug drug)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        drug.setCreateTime(DateUtils.getNowDate());
        drug.setCreateBy(user.getUserName());
        drug.setCreateName(user.getNickName());
        drug.setUpdateBy(user.getNickName());
        drug.setUpdateName(user.getUserName());
        drug.setUpdateTime(DateUtils.getNowDate());

        /*关联厂家ID 入库存储名称*/
        if (!Objects.isNull(drug.getManuId()) && Strings.isNotEmpty(drug.getManuId().trim())){
            String dictLabel = iSysDictDataService.selectDictLabel("drug_factory", drug.getManuId());
            drug.setManuName(dictLabel);
        }

        int i = drugMapper.insertDrug(drug);
        if(drug.getDrugDiseaseList().size()>0) {
            drug.getDrugDiseaseList().forEach(item->{
                item.setDrugId(drug.getId());
                drugDiseaseService.insertDrugDisease(item);
            });
        }
        return i;
    }

    /**
     * 修改药品
     * 
     * @param drug 药品
     * @return 结果
     */
    @Override
    public int updateDrug(Drug drug)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        drug.setUpdateBy(user.getNickName());
        drug.setUpdateName(user.getUserName());
        drug.setUpdateTime(DateUtils.getNowDate());

        /*关联厂家ID 入库存储名称*/
        if (!Objects.isNull(drug.getManuId()) && Strings.isNotEmpty(drug.getManuId().trim())){
            String dictLabel = iSysDictDataService.selectDictLabel("drug_factory", drug.getManuId());
            drug.setManuName(dictLabel);
        }

        //删除疾病关联信息
        if(drug.getDelDrugDiseaseIds().length>0){
            drugDiseaseService.deleteDrugDiseaseByIds(drug.getDelDrugDiseaseIds());
        }
        if(drug.getDrugDiseaseList().size()>0) {
            drug.getDrugDiseaseList().forEach(item->{
                if(item.getId()!= null){
                    drugDiseaseService.updateDrugDisease(item);
                }else{
                    item.setDrugId(drug.getId());
                    drugDiseaseService.insertDrugDisease(item);
                }
            });
        }

        return drugMapper.updateDrug(drug);

    }

    /**
     * 批量删除药品
     * 
     * @param ids 需要删除的药品主键
     * @return 结果
     */
    @Override
    public int deleteDrugByIds(Long[] ids)
    {
        return drugMapper.deleteDrugByIds(ids);
    }

    /**
     * 删除药品信息
     * 
     * @param id 药品主键
     * @return 结果
     */
    @Override
    public int deleteDrugById(Long id)
    {
        return drugMapper.deleteDrugById(id);
    }

    /**
     * 查询药品列表
     *
     * @param drug 药品
     * @return 药品集合
     */
    public List<Drug> getDrugList(Drug drug){
        return drugMapper.getDrugList(drug);
    }
}
